<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 6/25/17
 * Time: 6:14 PM
 */
declare(strict_types=1);


namespace wshore\library\quene;

use wshore\helper\Config;

/**
 * Class Deque 双向队列
 * @package lite\helper
 */
class Deque implements QueneInterface
{

    private $queue = [];

    public function __construct(string $name)
    {
        $file = WS_PATH_DATA . "/deque/{$name}.php";
        $this->queue = Config::parse($file, []);
        $quene = &$this->queue;
        register_shutdown_function(function () use ($file, $quene) {
            Config::store($file, $quene);
        });
    }


    /**
     * （尾部）入队
     * @param mixed $value
     * @return int
     */
    public function push($value)
    {
        return array_push($this->queue, $value);
    }

    /**
     * （尾部）出队
     * @return mixed
     */
    public function pop()
    {
        return array_pop($this->queue);
    }

    /**
     * （头部）入队
     * @param $value
     * @return int
     */
    public function unshift($value)
    {
        return array_unshift($this->queue, $value);
    }

    /**
     * （头部）出队
     * @return mixed
     */
    public function shift()
    {
        return array_shift($this->queue);
    }

    /**
     * 清空队列
     * @return void
     */
    public function clean()
    {
        unset($this->queue);
    }

    /**
     * 获取列头
     * @return mixed
     */
    public function first()
    {
        return reset($this->queue);
    }

    /**
     * 获取列尾
     * @return mixed
     */
    public function last()
    {
        return end($this->queue);
    }

    /**
     * 获取长度
     * @return int
     */
    public function length()
    {
        return count($this->queue);
    }


}